﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wpcCreater
{
    public partial class Form1 : Form
    {
        Tools T = new Tools();
        String tmp = "";

        public Form1()
        {
            InitializeComponent();
        }

        // 生成随机序列数
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int N = Int32.Parse(textBox1.Text.Trim());  // 序列数的位数

                tmp = getSerialN(N);
                textBox2.Text = tmp;
            }
            catch (Exception ex) { MessageBox.Show("序列数的位数最小为1"); }
        }

        // 文本全选
        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A && e.Control)
            {
                textBox2.SelectAll();
            }
        }

        // N位0-9之间的序列数生成算法，生成的序列数可用于密码的随机不重复破解
        public static string getSerialN(int N)
        {
            string[] serial = getSerial(10, -1, N);     // 获取0-9之间的N位序列数
            string tmp = "";
            foreach (string s in serial)
                tmp += s + "\r\n";

            return tmp;
        }

        /** 序列数生成算法，生成【1+offset，n+offset】之间的随机序列数组，每个数值出现且仅出现一次 */
        public static int[] getSerial(int n, int offset)
        {
            Random Rnd = new Random();

            int[] tmp = new int[n];
            int[] num1 = new int[n];
            for (int i0 = 1; i0 <= n; i0++)
                num1[i0 - 1] = i0 + offset;

            for (int i = num1.Length; i > 0; i--)
            {
                int index = Rnd.Next(i);

                // 随机选中一个数  
                tmp[i - 1] = num1[index];

                // 剔除选中的数值  
                int[] num2 = new int[i - 1];
                for (int j = 0; j < i; j++)
                    if (j < index)
                        num2[j] = num1[j];
                    else if (j > index) num2[j - 1] = num1[j];
                num1 = num2;
            }

            return tmp;
        }

        /** 序列数生成算法，生成由序列数组成的N位数, 一位序列数范围【1+offset，n+offset】*/
        public static string[] getSerial(int n, int offset, int N)
        {
            string[] S = new string[] { "" };
            for (int i = 0; i < N; i++)
            {
                int[] serial = getSerial(10, -1);// 生成0到9随机序列数
                S = addSerial(S, serial);
            }

            return S;
        }

        // 向已有的序列数组sBase中添加一位序列数
        public static string[] addSerial(string[] sBase, int[] serial)
        {
            string[] serials = new string[sBase.Length * serial.Length];

            int i = 0;
            foreach (string s1 in sBase)
                foreach (int s2 in serial)
                    serials[i++] = s1 + s2;
            return serials;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            String file = T.dragDrop(e);
            String[] files = file.Split(';');
            String data = T.ToString(files[0]);
            textBox2.Text = data;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            T.dragEnter(e);
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String data = tmp.Replace("\r", "");
            String filePath = T.SaveProcess(data, "wpc", "");
            T.MessageWithOpen("已保存为文件，是否打开？", filePath);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String Dir = T.getCurDir("");
            System.Diagnostics.Process.Start("explorer.exe", "/e,/select, " + Dir);
        }

        private void 生成wpcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tmp = "0\r\n0\r\n0\r\n" + getSerialN(4) + getSerialN(3);
            textBox2.Text = tmp;

            String data = tmp.Replace("\r", "");
            String filePath = T.SaveProcess(data, "WPC.wpc", "");
            T.MessageWithOpen("已保存为文件，是否打开？", filePath);
        }

        
    }
}
